package com.leetcode.partition3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/10/22 8:01
 */
public class LC229求众数2 {

    public static List<Integer> majorityElement(int[] nums) {
        return hashtable(nums);
    }

    public static void main(String[] args) {
        System.out.println(majorityElement(new int[]{3, 2, 3}));
    }

    private static List<Integer> hashtable(int[] nums) {
        double times = nums.length / 3.0;
        System.out.println(times);
        Map<Integer, Integer> numToTimes = new HashMap<>();
        List<Integer> answer = new ArrayList<>();
        for (int num : nums) {
            numToTimes.put(num, numToTimes.getOrDefault(num, 0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry : numToTimes.entrySet()) {
            if (entry.getValue() > times) {
                answer.add(entry.getKey());
            }
        }
        return answer;
    }
}
